package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Vector;

public class GetWorkHoursByIdEmployee {
	
	private String empID;
	private String thisMonth;
	private Vector<Vector> totalEmpHours;
	
	
	public GetWorkHoursByIdEmployee(String id,String month)
	{
		this.empID=id;
		this.thisMonth=month;
		
	}
	
	
	public Vector<Vector> getWorkHoursById()
	{
		totalEmpHours=new Vector<Vector>();
		SQLConnection sc = new SQLConnection();	
		
		try {
			PreparedStatement ps = sc.getConnection().prepareStatement("SELECT emp.name,ewh.date,ewh.clock_in,ewh.clock_out,ewh.comments  FROM employee emp,employee_working_hours ewh  WHERE emp.eid=? AND ewh.eid=? AND MONTH(date)=? ;");
			ps.setString(1,empID);
			ps.setString(2,empID);
			ps.setString(3,thisMonth);
			ResultSet rs=ps.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			
			 
			 while(rs.next())
		        {   
				    
				 Vector<String> totalHoursToday=new Vector<String>();
				 totalHoursToday.add(empID);	
		         for(int col = 1; col <= rsmd.getColumnCount(); col++) 
		         	{                	                
		                int type = rsmd.getColumnType(col);
		               
		                switch(type)
		                {
		                      
		                  case Types.NULL :
		                	   
	                    	   totalHoursToday.add(rs.getNString(col));
	                    	  
	                            break;
	                        
		                    case Types.INTEGER :
		                    	  
		                    	totalHoursToday.add(new Integer(rs.getInt(col)).toString());
		                    	
		                    	break;
		                        
		                    case Types.VARCHAR :
		                    	 
		                    	totalHoursToday.add(rs.getString(col));
		                    	
		                        break;
		                    case Types.DATE :
		                    	 
		                    	totalHoursToday.add(rs.getDate(col).toString());
		                    	
		                        break;
		                    case Types.TIME :
		                    	 
		                        // here is the problem [solved]
		                    	
		                    	 if(rs.getTime(col)==null)     //	if(rs.getTime(col).equals(null))
		                    	{   
		                    		 
		                  	    	totalHoursToday.add("__:__:__");
		                    	}
		                   	else
		                    	{   
		                    	totalHoursToday.add(rs.getTime(col).toString());
		                    	 
		                    	}
		                        break;    
		                        
		                }//switch
		                
		                
		         	}//for
		       
		         totalEmpHours.add(totalHoursToday); 
		        }//while
			 
			 ps.close();
	         rs.close();
	         sc.getConnection().close();
		}//try
	              catch (SQLException e) 
	              {
	                  e.printStackTrace();
	              }
		
		return totalEmpHours;
		
	}

}
